function [] = ZP_vel_rotate(indir0,outdir0,period_list)
% ZP_vel_rotate.m 9/7/2011 Parker MacCready
%
% rotates the u10 and v10 vecotrs so they are in the east and north
% directions, instead of aligned to the native MM5 grid

disp('--- ZP_vel_rotate ---')

g_list = {'d2';'d3'};

for pp = 1:length(period_list)
    period = period_list{pp};
    disp(['period = ',period])
    for gg = 1:length(g_list)
        g_tag = g_list{gg};
        indir = [indir0,period,'/'];
        outdir = [outdir0,period,'/'];
        
        load([indir,'u10_',g_tag,'.mat']);
        vmat = load([indir,'v10_',g_tag,'.mat']);
        u10 = outmat; v10 = vmat.outmat;
        [NT,NR,NC] = size(u10);
        theta = NaN * lon;
        for rr = 1:NR
            [dist,theta(rr,1:end-1)] = sw_dist(lat(rr,:),lon(rr,:),'km');
        end
        theta(:,end) = theta(:,end-1); % pad the end
        alpha = - pi*theta/180; % alpha is the angle (radians) to rotate the
        % coordinate system to go from the MM5 grid to East-North
        sa = sin(alpha); ca = cos(alpha);
        ssa = NaN * u10; cca = NaN * u10;
        for tt = 1:NT
            ssa(tt,:,:) = sa;
            cca(tt,:,:) = ca;
        end
        % calculate and save u10r
        outmat = u10.*cca + v10.*ssa;
        save([outdir,'u10r','_',g_tag,'.mat'],'lon','lat','tdvec', ...
            'ts1_vec','ts2_vec','outmat','-v7.3');
        % calculate and save v10r
        outmat = v10.*cca - u10.*ssa;
        save([outdir,'v10r','_',g_tag,'.mat'],'lon','lat','tdvec', ...
            'ts1_vec','ts2_vec','outmat','-v7.3');
        
    end % end of gg loop
    
end
